Page({
  data: {
    dialogVisible: false,
    isLogin: wx.getStorageSync('token'),
    houseList: [],
    currentId: ''
  },

  onShow() {
    this.getHouseList()
  },

  goDetail(ev) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + ev.mark.id,
    })
  },

  async getHouseList() {
    // 请求数据接口
    const { code, data: houseList } = await wx.http.get('/room')
    // 检测接口返回的结果
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ houseList })
    console.log(houseList);
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },

  delHouse(ev) {
    this.data.currentId = ev.mark.id
    this.setData({
      dialogVisible: true,
    })
  },

  async dialogClose(ev) {
    console.log(ev.detail);
    if (ev.detail === 'confirm') {
      // 发请求删除
      const { code } = await wx.http.delete('/room/' + this.data.currentId)
      // 检测接口调用结果
      if (code !== 10000) return wx.utils.toast('删除房屋失败!')
      // 更新房屋列表
      const newList = this.data.houseList.filter(item => item.id !== this.data.currentId)
      this.setData({
        houseList: newList,
      })
    }
  }
})
